﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using Common;
using System.Threading;

namespace Indexing
{
    public class Downloader
    {
        List<string> _urls;
        Site _site;
        Hashtable urlHtmlPage;
        int _maxThreads;
        DateTime _fromDate;
        //Locks  for concurrency.
        object lockUrls = new object();
        object lockurlHtmlPage = new object();

        public Downloader(List<string> urlsToDownload, DateTime fromDate, Site site, int maxThreads)
        {
            _urls = urlsToDownload;
            _site = site;
            urlHtmlPage = new Hashtable();
            _maxThreads = maxThreads;
            _fromDate = fromDate;
        }

        /// <summary>
        /// spawns #maxThread threads in order to download all pages in the list of urls.
        /// </summary>
        /// <returns></returns>
        public Hashtable DownloadUrls()
        {
            Thread[] threads = new Thread[_maxThreads];
            for (int i = 0; i < threads.Length; i++)
            {
                threads[i] = new Thread(new ParameterizedThreadStart(download));
                threads[i].Name = "DownloaderThread #" + i.ToString();
                threads[i].Start();
            }
            for (int i = 0; i < threads.Length; ++i)
            {
                //wait for the downloading to end.
                threads[i].Join();
            }
            return urlHtmlPage;
        }

        private void download(object obj)
        {

        }
    }
}

